package com.obas.access.business.service.impl;

import java.util.List;

import com.ease.common.vo.MapParameter;
import com.ease.platform.access.service.impl.AbstractNamespaceAccessServiceImpl;
import com.obas.access.business.service.SystemProjectAccessService;
import com.obas.access.business.service.SystemScenarioAccessService;
import com.obas.model.business.SystemProject;
import com.obas.model.business.SystemScenario;

public class SystemProjectAccessServiceImpl extends AbstractNamespaceAccessServiceImpl<SystemProject> implements SystemProjectAccessService {

	static final class Statement {
		public static final String disassociateAllSystemScenario = "disassociateAllSystemScenario";
	}

	private SystemScenarioAccessService systemScenarioAccessService;

	public SystemProject remove(SystemProject systemProject) {
		if (null != systemProject) {

			long systemProjectId = systemProject.getId();
			//
			disassociateAll(systemProjectId, systemProject);
			//
			systemProject = super.remove(systemProject);
			//
			removeCascade(systemProjectId, systemProject);
		}
		return systemProject;
	}

	public void removeCascade(long systemProjectId, SystemProject systemProject) {
		{/* TBD */
			List<SystemScenario> systemScenarios = systemScenarioAccessService.findBySystemProjectId(systemProjectId);
			if (notEmpty(systemScenarios)) {
				for (SystemScenario systemScenario : systemScenarios) {
					systemScenarioAccessService.remove(systemScenario);
				}
			}
		}
	}

	public void disassociateAll(long systemProjectId, SystemProject systemProject) {
		disassociateAllSystemScenario(systemProjectId);
	}

	public void disassociateAllSystemScenario(long systemProjectId) {
		MapParameter parameter = new MapParameter();
		parameter.put("systemProjectId", systemProjectId);
		this.update(Statement.disassociateAllSystemScenario, parameter);
	}

	/**
	 * @return the systemScenarioAccessService
	 */
	public SystemScenarioAccessService getSystemScenarioAccessService() {
		return systemScenarioAccessService;
	}

	/**
	 * @param systemScenarioAccessService
	 *            the systemScenarioAccessService to set
	 */
	public void setSystemScenarioAccessService(SystemScenarioAccessService systemScenarioAccessService) {
		this.systemScenarioAccessService = systemScenarioAccessService;
	}

}
